const router = require("koa-router")()
const UserModel = require("../../models/user")
const MovieModel = require("../../models/movie")
router.get('/api/history',async ctx=>{
    var {id,m} = ctx.query
    console.log(id,m)
    try{
        var data = await  MovieModel(m).find({_id:id})
        var item = data[0]
        /* 判断history中是否有这条数据 */
        var isHistory = await UserModel.find({"history._id":id})
        if(isHistory.length==0){
            await UserModel.updateOne({},{$push:{history:item}})
            ctx.body={
                code:200,
                msg:"添加到历史记录"
            }
        }else{
            ctx.body={
                code:400,
                msg:"历史记录中已存在"
            }
        }
    }catch(err){
        ctx.body={
            code:400,
            msg:"输入的参数不合法"
        }
    }
})
module.exports = router